home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / APXMDI.PAK / APXMDAPP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  9.0 KB  |  340 lines

  1. //----------------------------------------------------------------------------
  2. //  Project ApxMdi
  3. //  Borland International
  4. //  Copyright ⌐ 1996. All Rights Reserved.
  5. //
  6. //  SUBSYSTEM:    ApxMdi Application
  7. //  FILE:         apxmdapp.cpp
  8. //  AUTHOR:
  9. //
  10. //  OVERVIEW
  11. //  ~~~~~~~~
  12. //  Source file for implementation of TApxMdiApp (TApplication).
  13. //
  14. //----------------------------------------------------------------------------
  15.  
  16. #include <owl/pch.h>
  17.  
  18. #include <owl/buttonga.h>
  19. #include <owl/statusba.h>
  20. #include <classlib/cmdline.h>
  21. #include <winsys/registry.h>
  22.  
  23. #include "apxmdapp.h"
  24. #include "apxmmdic.h"
  25. #include "apxmdabd.h"                        // Definition of about dialog.
  26.  
  27. //{{TApxMdiApp Implementation}}
  28.  
  29.  
  30. //
  31. // Build a response table for all messages/commands handled by the application.
  32. //
  33. DEFINE_RESPONSE_TABLE2(TApxMdiApp, TRecentFiles, TApplication)
  34. //{{TApxMdiAppRSP_TBL_BEGIN}}
  35.   EV_COMMAND(CM_HELPABOUT, CmHelpAbout),
  36.   EV_WM_DROPFILES,
  37.   EV_WM_WININICHANGE,
  38.   EV_REGISTERED(MruFileMessage, CmFileSelected),
  39. //{{TApxMdiAppRSP_TBL_END}}
  40. END_RESPONSE_TABLE;
  41.  
  42.  
  43. //--------------------------------------------------------
  44. // TApxMdiApp
  45. // ~~~~~
  46. //
  47. TApxMdiApp::TApxMdiApp() : TApplication("AppExpert MDI Example"), TRecentFiles(".\\ApxMdi.ini", 4)
  48. {
  49.  
  50.   Printer = 0;
  51.   Printing = 0;
  52.  
  53.   // INSERT>> Your constructor code here.
  54. }
  55.  
  56.  
  57. TApxMdiApp::~TApxMdiApp()
  58. {
  59.   delete Printer;
  60.  
  61.   // INSERT>> Your destructor code here.
  62. }
  63.  
  64.  
  65. void TApxMdiApp::SetupSpeedBar(TDecoratedMDIFrame* frame)
  66. {
  67.   ApxHarbor = new THarbor(*frame);
  68.  
  69.   // Create default toolbar New and associate toolbar buttons with commands.
  70.   //
  71.   TDockableControlBar* cb = new TDockableControlBar(frame);
  72.   cb->Insert(*new TButtonGadget(CM_MDIFILENEW, CM_MDIFILENEW));
  73.   cb->Insert(*new TButtonGadget(CM_MDIFILEOPEN, CM_MDIFILEOPEN));
  74.   cb->Insert(*new TButtonGadget(CM_FILESAVE, CM_FILESAVE));
  75.   cb->Insert(*new TSeparatorGadget(6));
  76.   cb->Insert(*new TButtonGadget(CM_EDITCUT, CM_EDITCUT));
  77.   cb->Insert(*new TButtonGadget(CM_EDITCOPY, CM_EDITCOPY));
  78.   cb->Insert(*new TButtonGadget(CM_EDITPASTE, CM_EDITPASTE));
  79.   cb->Insert(*new TSeparatorGadget(6));
  80.   cb->Insert(*new TButtonGadget(CM_EDITUNDO, CM_EDITUNDO));
  81.   cb->Insert(*new TSeparatorGadget(6));
  82.   cb->Insert(*new TButtonGadget(CM_EDITFIND, CM_EDITFIND));
  83.   cb->Insert(*new TButtonGadget(CM_EDITFINDNEXT, CM_EDITFINDNEXT));
  84.   cb->Insert(*new TSeparatorGadget(6));
  85.   cb->Insert(*new TButtonGadget(CM_FILEPRINT, CM_FILEPRINT));
  86.   cb->Insert(*new TButtonGadget(CM_FILEPRINTPREVIEW, CM_FILEPRINTPREVIEW));
  87.  
  88.   // Add caption and fly-over help hints.
  89.   //
  90.   cb->SetCaption("Toolbar");
  91.   cb->SetHintMode(TGadgetWindow::EnterHints);
  92.  
  93.   ApxHarbor->Insert(*cb, alTop);
  94. }
  95.  
  96.  
  97. //--------------------------------------------------------
  98. // TApxMdiApp
  99. // ~~~~~
  100. // Application intialization.
  101. //
  102. void TApxMdiApp::InitMainWindow()
  103. {
  104.   if (nCmdShow != SW_HIDE)
  105.     nCmdShow = (nCmdShow != SW_SHOWMINNOACTIVE) ? SW_SHOWNORMAL : nCmdShow;
  106.  
  107.   MdiClient = new TApxMdiMDIClient;
  108.   TDecoratedMDIFrame* frame = new TDecoratedMDIFrame(Name, IDM_MDI, *MdiClient, true);
  109.  
  110.   // Enable acceptance of dropped files
  111.   //
  112.   frame->Attr.ExStyle |= WS_EX_ACCEPTFILES;
  113.  
  114.   // Assign icons for this application.
  115.   //
  116.   frame->SetIcon(this, IDI_MDIAPPLICATION);
  117.   frame->SetIconSm(this, IDI_MDIAPPLICATION);
  118.  
  119.   // Associate with the accelerator table.
  120.   //
  121.   frame->Attr.AccelTable = IDM_MDI;
  122.  
  123.   TStatusBar* sb = new TStatusBar(frame, TGadget::Recessed,
  124.                                   TStatusBar::CapsLock        |
  125.                                   TStatusBar::NumLock         |
  126.                                   TStatusBar::ScrollLock);
  127.   frame->Insert(*sb, TDecoratedFrame::Bottom);
  128.  
  129.   SetupSpeedBar(frame);
  130.  
  131.   SetMainWindow(frame);
  132.  
  133.   frame->SetMenuDescr(TMenuDescr(IDM_MDI));
  134. }
  135.  
  136.  
  137. //--------------------------------------------------------
  138. // TApxMdiApp
  139. // ~~~~~
  140. // Application instance initialization.
  141. //
  142. void TApxMdiApp::InitInstance()
  143. {
  144.   TApplication::InitInstance();
  145.  
  146.   ProcessCmdLine(lpCmdLine);
  147. }
  148.  
  149.  
  150. //--------------------------------------------------------
  151. // TApxMdiApp
  152. // ~~~~~
  153. // Process command line parameters.
  154. //
  155. void TApxMdiApp::ProcessCmdLine(char * CmdLine)
  156. {
  157.   TCmdLine cmd(CmdLine);
  158.  
  159.   while (cmd.Kind != TCmdLine::Done) {
  160.     if (cmd.Kind == TCmdLine::Option) {
  161.       if (strnicmp(cmd.Token, "unregister", cmd.TokenLen) == 0) {
  162.         UnRegisterInfo();
  163.         return;
  164.       }
  165.     }
  166.     cmd.NextToken();
  167.   }
  168.  
  169.   RegisterInfo();
  170. }
  171.  
  172.  
  173. //--------------------------------------------------------
  174. // TApxMdiApp
  175. // ~~~~~
  176. // Register application info.
  177. //
  178. void TApxMdiApp::RegisterInfo()
  179. {
  180.   TAPointer<char> buffer = new char[_MAX_PATH];
  181.  
  182.   GetModuleFileName(buffer, _MAX_PATH);
  183.  
  184.   TRegKey(TRegKey::ClassesRoot, "ApxMdi.Application\\DefaultIcon").SetDefValue(0, REG_SZ, buffer, strlen(buffer));
  185.   strcat(buffer, ",1");
  186.   TRegKey(TRegKey::ClassesRoot, "ApxMdi.Document.1\\DefaultIcon").SetDefValue(0, REG_SZ, buffer, strlen(buffer));
  187.   strcpy(buffer, "ApxMdi.Document.1");
  188.   TRegKey(TRegKey::ClassesRoot, ".txt").SetDefValue(0, REG_SZ, buffer, strlen(buffer));
  189. }
  190.  
  191.  
  192. //--------------------------------------------------------
  193. // TApxMdiApp
  194. // ~~~~~
  195. // Unregister application info.
  196. //
  197. void TApxMdiApp::UnRegisterInfo()
  198. {
  199.   TAPointer<char> buffer = new char[_MAX_PATH];
  200.  
  201.   GetModuleFileName(buffer, _MAX_PATH);
  202.  
  203.   TRegKey(TRegKey::ClassesRoot, "ApxMdi.Application").DeleteKey("DefaultIcon");
  204.   TRegKey(TRegKey::ClassesRoot, "ApxMdi.Document.1").DeleteKey("DefaultIcon");
  205.  
  206.   TRegKey::ClassesRoot.DeleteKey("ApxMdi.Application");
  207.   TRegKey::ClassesRoot.DeleteKey("ApxMdi.Document.1");
  208.   TRegKey::ClassesRoot.DeleteKey(".txt");
  209. }
  210.  
  211.  
  212.  
  213.  
  214.  
  215. //--------------------------------------------------------
  216. // TApxMdiApp
  217. // ~~~~~~~~~~~
  218. // Menu Help About ApxMdi command
  219. //
  220. void TApxMdiApp::CmHelpAbout()
  221. {
  222.   // Show the modal dialog.
  223.   //
  224.   TApxMdiAboutDlg(MainWindow).Execute();
  225. }
  226.  
  227.  
  228. void TApxMdiApp::EvDropFiles(TDropInfo drop)
  229. {
  230.   TFileDropletList files;
  231.  
  232.   // Iterate thru the entries in drop and create FileDrops objects for each
  233.   // one.
  234.   //
  235.   int fileCount = drop.DragQueryFileCount();  // Number of files dropped.
  236.   for (int i = 0; i < fileCount; i++)
  237.     files.Add(new TFileDroplet(drop, i));
  238.  
  239.   // Open the files that were dropped.
  240.   //
  241.   AddFiles(files);
  242.  
  243.   // Release the memory allocated for this handle with DragFinish.
  244.   //
  245.   drop.DragFinish();
  246. }
  247.  
  248.  
  249. void TApxMdiApp::AddFiles(TFileDropletList& files)
  250. {
  251.   TApxMdiMDIClient* theClient = TYPESAFE_DOWNCAST(GetMainWindow()->GetClientWindow(), TApxMdiMDIClient);
  252.  
  253.   if (theClient) {
  254.     // Open all files dragged in.
  255.     //
  256.     for (TFileDropletListIter fileIter(files); fileIter; fileIter++)
  257.       theClient->OpenFile(fileIter.Current()->GetName());
  258.   }
  259. }
  260.  
  261.  
  262. int32 TApxMdiApp::CmFileSelected(uint wp, int32)
  263. {
  264.   TAPointer<char> text = new char[_MAX_PATH];
  265.  
  266.   GetMenuText(wp, text, _MAX_PATH);
  267.  
  268.   TApxMdiMDIClient* theClient = TYPESAFE_DOWNCAST(GetMainWindow()->GetClientWindow(), TApxMdiMDIClient);
  269.  
  270.   if (theClient)
  271.     theClient->OpenFile(text);
  272.   return 0;
  273. }
  274.  
  275.  
  276. void TApxMdiApp::EvWinIniChange(char far* section)
  277. {
  278.   if (strcmp(section, "windows") == 0) {
  279.     // If the device changed in the WIN.INI file then the printer
  280.     // might have changed.  If we have a TPrinter(Printer) then
  281.     // check and make sure it's identical to the current device
  282.     // entry in WIN.INI.
  283.     //
  284.     if (Printer) {
  285.       const int bufferSize = 255;
  286.       char printDBuffer[bufferSize];
  287.       LPSTR printDevice = printDBuffer;
  288.       LPSTR devName;
  289.       LPSTR driverName = 0;
  290.       LPSTR outputName = 0;
  291.       if (::GetProfileString("windows", "device", "", printDevice, bufferSize)) {
  292.         // The string which should come back is something like:
  293.         //
  294.         //      HP LaserJet III,hppcl5a,LPT1:
  295.         //
  296.         // Where the format is:
  297.         //
  298.         //      devName,driverName,outputName
  299.         //
  300.         devName = printDevice;
  301.         while (*printDevice) {
  302.           if (*printDevice == ',') {
  303.             *printDevice++ = 0;
  304.             if (!driverName)
  305.               driverName = printDevice;
  306.             else
  307.               outputName = printDevice;
  308.           }
  309.           else
  310.             printDevice = ::AnsiNext(printDevice);
  311.         }
  312.  
  313.         if (Printer->GetSetup().Error != 0                ||
  314.             strcmp(devName, Printer->GetSetup().GetDeviceName()) != 0  ||
  315.             strcmp(driverName, Printer->GetSetup().GetDriverName()) != 0 ||
  316.             strcmp(outputName, Printer->GetSetup().GetOutputName()) != 0 ) {
  317.           // New printer installed so get the new printer device now.
  318.           //
  319.           delete Printer;
  320.           Printer = new TPrinter(this);
  321.         }
  322.       }
  323.       else {
  324.         // No printer installed(GetProfileString failed).
  325.         //
  326.         delete Printer;
  327.         Printer = new TPrinter(this);
  328.       }
  329.     }
  330.   }
  331. }
  332.  
  333.  
  334. int OwlMain(int , char* [])
  335. {
  336.   TApxMdiApp   app;
  337.   return app.Run();
  338. }
  339.  
  340.